package com.simplehc7878.warning.task;

import com.simplehc7878.warning.domain.model.WarningRule;
import com.simplehc7878.warning.service.WarningRuleService;
import com.simplehc7878.warning.strategy.impl.StrategyGroup;
import com.simplehc7878.warning.strategy.WarningStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Component
public class WarningTask implements ApplicationContextAware {


    @Autowired
    private WarningRuleService warningRuleService;

    @Autowired
    private ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public void check() {
        List<WarningRule> warningRules = warningRuleService.ruleList();
        if (CollectionUtils.isEmpty(warningRules)) {
            return;
        }
        for (WarningRule rule : warningRules) {
            List<WarningStrategy> strategyList = StrategyGroup.init(rule, context);
            if (!CollectionUtils.isEmpty(strategyList)) {
                strategyList.forEach(v -> v.run(rule));
            }
        }
    }

}
